package daiwei.javaWeb.exam.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import daiwei.javaWeb.exam.meta.Product;
import daiwei.javaWeb.exam.meta.ShowProduct;
import daiwei.javaWeb.exam.meta.User;
import daiwei.javaWeb.exam.serviceImpl.DataBaseOPImpl;

@Controller
public class MainController {
	
	@Autowired
	private DataBaseOPImpl dataBaseOpreation;
		
	@RequestMapping(value="/", method=RequestMethod.GET)
	public String index(HttpSession session) throws Exception {
		List<Product> products =dataBaseOpreation.getContents();
		User user = (User)session.getAttribute("user");
		if(user!=null) {
			for(Product product:products) {
				if(user.getUsertype()==0&&dataBaseOpreation.getBoughtProduct(user.getId(), product.getId())!=null) {
					product.setIsBuy(true);
				} else if(user.getUsertype()==1&&dataBaseOpreation.getSelledProductId(product.getId())!=null){
					product.setIsSell(true);
				}
			}
		}
		session.setAttribute("productList", products);
		return "index";
	}
	
	@RequestMapping(value="/login", method=RequestMethod.GET)
	public String login() {
		return "login";
	}
	
	@RequestMapping(value="/logout", method=RequestMethod.GET)
	public void  logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.getSession().invalidate();
		resp.sendRedirect("/");
	}
	
	@RequestMapping(value="/account", method=RequestMethod.GET)
	public String Account( HttpSession session) throws Exception {
		session.setAttribute("buyList",dataBaseOpreation.getAccount(((User) session.getAttribute("user")).getId()));
		return "account";
	}
	
	@RequestMapping(value="/public", method=RequestMethod.GET)
	public String publish() {
		return "public";
	}
	
	@RequestMapping(value="/publicSubmit", method=RequestMethod.POST)
	public String publishSubmit(@RequestParam("title") String title,@RequestParam("summary") String summary, HttpSession session,
					@RequestParam("detail") String detail, @RequestParam("image") String imageAddr, @RequestParam("price") double price) {
		Product p = new Product(price*10000,title,imageAddr,summary,detail);
		try {
			p.setId(dataBaseOpreation.addProductAndGetLastId(p));
			session.setAttribute("product", p);
		} catch (Exception e) {
		
		}
		return "publicSubmit";
	}
	
	@RequestMapping(value="/show",method=RequestMethod.GET)
	public String show(@RequestParam int id,HttpSession session) throws Exception {
		ShowProduct p = dataBaseOpreation.getShowProduct(id,session);
		p.setId(id);
		session.setAttribute("product", p);
		return "show";
	}
	
	
	@RequestMapping(value="/edit",method=RequestMethod.GET)
	public String edit(@RequestParam int id, HttpSession session ) throws Exception {
		Product p = dataBaseOpreation.getContent(id);
		session.setAttribute("product", p);
		return "edit";
	}
	
	@RequestMapping(value="/editSubmit",method=RequestMethod.POST)
	public String editSubmit(@RequestParam int id,@RequestParam String title,@RequestParam String summary, HttpSession session,
			@RequestParam String detail, @RequestParam String image, @RequestParam double price) {
		if( price < 0 ) {
			price =0.0;
		}
		Product product = new Product(price*10000, title, image, summary, detail);
		product.setId(id);
		try {
			dataBaseOpreation.editProduct(product);
			session.setAttribute("product", product);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "editSubmit";
	}

	
}
